package com.cw.jeeyt.service.converter.security;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.cw.jeeyt.service.bo.security.SecurityRoleBo;
import com.cw.jeeyt.service.bo.security.SecurityRoleVo;
import com.cw.jeeyt.dao.domain.security.SecurityRoleModel;
import com.cw.jeeyt.dao.model.security.SecurityRoleDo;
import com.cw.lang.common.utils.CollectionUtil;

/**
 * 角色信息转换类
 *
 * @author chenw
 * @date 2019-03-10 13:01:34
 */
public class SecurityRoleConverter {

    /**
     * do转bo
     *
     * @param model
     * @return
     */
    public static SecurityRoleBo do2Bo(SecurityRoleDo model) {
        if (model == null) {
            return null;
        }
        SecurityRoleBo bo = new SecurityRoleBo();
        bo.setId(model.getId());
        bo.setName(model.getName());
        bo.setComment(model.getComment());
        bo.setParentId(model.getParentId());
        bo.setOrgId(model.getOrgId());
        bo.setRoleType(model.getRoleType());
        return bo;
    }

    /**
     * bo转do
     *
     * @param bo
     * @return
     */
    public static SecurityRoleDo bo2Do(SecurityRoleBo bo) {
        if (bo == null) {
            return null;
        }
        SecurityRoleDo model = new SecurityRoleDo();
        model.setId(bo.getId());
        model.setName(bo.getName());
        model.setComment(bo.getComment());
        model.setParentId(bo.getParentId());
        model.setOrgId(bo.getOrgId());
        model.setRoleType(bo.getRoleType());
        return model;
    }

    /**
     * vo转model
     *
     * @param bo
     * @return
     */
    public static SecurityRoleModel vo2Model(SecurityRoleVo bo) {
        if (bo == null) {
            return null;
        }
        SecurityRoleModel model = new SecurityRoleModel();
        model.setId(bo.getId());
        model.setName(bo.getName());
        model.setComment(bo.getComment());
        model.setParentId(bo.getParentId());
        model.setOrgId(bo.getOrgId());
        model.setRoleType(bo.getRoleType());
        if (CollectionUtil.isNotEmpty(model.getRoleUsers())) {
            model.setRoleUsers(SecurityUserConverter.boList2DoList(bo.getRoleUsers()));
        }
        return model;
    }

    /**
     * model转vo
     *
     * @param model
     * @return
     */
    public static SecurityRoleVo model2Vo(SecurityRoleModel model) {
        if (model == null) {
            return null;
        }
        SecurityRoleVo roleVo = new SecurityRoleVo();
        roleVo.setId(model.getId());
        roleVo.setName(model.getName());
        roleVo.setComment(model.getComment());
        roleVo.setParentId(model.getParentId());
        roleVo.setOrgId(model.getOrgId());
        roleVo.setRoleType(model.getRoleType());
        if (CollectionUtil.isNotEmpty(model.getRoleUsers())) {
            roleVo.setRoleUsers(SecurityUserConverter.doList2BoList(model.getRoleUsers()));
        }
        return roleVo;
    }

    /**
     * model转bo
     *
     * @param model
     * @return
     */
    public static SecurityRoleBo model2Bo(SecurityRoleModel model) {
        if (model == null) {
            return null;
        }
        SecurityRoleBo bo = new SecurityRoleBo();
        bo.setId(model.getId());
        bo.setName(model.getName());
        bo.setComment(model.getComment());
        bo.setParentId(model.getParentId());
        bo.setOrgId(model.getOrgId());
        bo.setRoleType(model.getRoleType());
        if (CollectionUtil.isNotEmpty(model.getRoleUsers())) {
            bo.setUsers(new HashSet(model.getRoleUsers()));
        }
        return bo;
    }

    /**
     * bo转model
     *
     * @param bo
     * @return
     */
    public static SecurityRoleModel bo2Model(SecurityRoleBo bo) {
        if (bo == null) {
            return null;
        }
        SecurityRoleModel model = new SecurityRoleModel();
        model.setId(bo.getId());
        model.setName(bo.getName());
        model.setComment(bo.getComment());
        model.setParentId(bo.getParentId());
        model.setOrgId(bo.getOrgId());
        model.setRoleType(bo.getRoleType());
        if (CollectionUtil.isNotEmpty(bo.getUsers())) {
            model.setRoleUsers(new ArrayList(bo.getUsers()));
        }
        return model;
    }

    /**
     * doList转boList
     *
     * @param doList
     * @return
     */
    public static List<SecurityRoleBo> doList2BoList(List<SecurityRoleDo> doList) {
        if (CollectionUtil.isEmpty(doList)) {
            return new ArrayList<>();
        }
        return Lists.newArrayList(Iterators.transform(doList.iterator(), SecurityRoleConverter::do2Bo));
    }

    /**
     * boList转doList
     *
     * @param boList
     * @return
     */
    public static List<SecurityRoleDo> boList2DoList(List<SecurityRoleBo> boList) {
        if (CollectionUtil.isEmpty(boList)) {
            return new ArrayList<>();
        }
        return Lists.newArrayList(Iterators.transform(boList.iterator(), SecurityRoleConverter::bo2Do));
    }

    /**
     * modelList转boList
     *
     * @param modelList
     * @return
     */
    public static List<SecurityRoleBo> modelList2BoList(List<SecurityRoleModel> modelList) {
        if (CollectionUtil.isEmpty(modelList)) {
            return new ArrayList<>();
        }
        return Lists.newArrayList(Iterators.transform(modelList.iterator(), SecurityRoleConverter::model2Bo));
    }

    /**
     * boList转modelList
     *
     * @param boList
     * @return
     */
    public static List<SecurityRoleModel> boList2ModelList(List<SecurityRoleBo> boList) {
        if (CollectionUtil.isEmpty(boList)) {
            return new ArrayList<>();
        }
        return Lists.newArrayList(Iterators.transform(boList.iterator(), SecurityRoleConverter::bo2Model));
    }

    public static ArrayList<SecurityRoleVo> modelList2VoList(List<SecurityRoleModel> roleModels) {
        if (CollectionUtil.isEmpty(roleModels)) {
            return new ArrayList<>();
        }
        return Lists.newArrayList(Iterators.transform(roleModels.iterator(), SecurityRoleConverter::model2Vo));
    }
}
